<?php
global $SMARTY,$DBobject,$CONFIG;

if (isset($_POST['ajax']) && !empty($_POST['ajax']) && $_POST['ajax']) {
    //根据主标题查询文章
    $array = [];
    $pageNow = $_POST['currentPage'];   //当前页码
    $titleId = $_POST['tabId'];         //标签id
    $pageSize = $_POST['pageSize'];     //每页新闻条数
    if (empty($pageNow) || empty($titleId) || empty($pageSize)) {
        echo json_encode([
            'err' => '0',
            'data' => '数据请求失败,请重新请求'
        ]);
        die();
    } else {
        $start=($pageNow-1)*$pageSize;
        $sqlArticle = "SELECT * FROM tbl_news_management  WHERE deleted IS NULL AND publish = '1' AND news_title_id = $titleId ORDER BY modified DESC LIMIT $start,$pageSize";
        $articleInfo = $DBobject->executeSQL($sqlArticle);
        $sql = "SELECT COUNT(*) FROM tbl_news_management  WHERE deleted IS NULL AND publish = '1' AND news_title_id = $titleId  ";
        $articleCount = $DBobject->executeSQL($sql);
        $count = unclean($articleCount)['0']['COUNT(*)']; //总数
        $totalPages = $count % $pageSize == 0 ? $count / $pageSize : ceil($count / $pageSize) ;
        $array['data']=unclean($articleInfo);
        $array['page'] = [
            'currentPage' => $pageNow,
            'pageSize' => $pageSize,
            'totalPage' => $totalPages,
        ];
        echo json_encode([
            'err' => '1',
            'data' => $array
        ]);
        die();
    }
} else {
    //主标题查询
    $sqlTitle = "SELECT * FROM tbl_news_title WHERE deleted IS NULL ORDER BY modified DESC";
    $tabs = $DBobject->executeSQL($sqlTitle);

    //根据主标题初始化新闻列表
    $array = [];
    foreach ($tabs as $key=>$re) {
        $news_title_id = $re['id'];
        $sql = "SELECT * FROM tbl_news_management WHERE deleted IS NULL AND publish = '1' AND news_title_id = $news_title_id ORDER BY modified DESC LIMIT 0,1 ";
        $articleInfo = $DBobject->executeSQL($sql);
        //剔除空标签
        if(empty(unclean($articleInfo))){
            unset($tabs[$key]);
            continue;
        }
        $array[$news_title_id]=unclean($articleInfo);
    }
    $SMARTY->assign("tabs", unclean($tabs));
    $tabId = $_GET['tabId'];
    $SMARTY->assign("tabId", $tabId);
}
